package com.laboratory.report.lab03;

import lombok.SneakyThrows;

import javax.swing.*;
import java.util.Collections;
import java.util.Vector;

/**
 * @author tangheng
 */
public class ButtonPannel extends JPanel {

    private OlympicTable olympicTable;

    public ButtonPannel(OlympicTable olympicTable) {
        this.olympicTable = olympicTable;

        add(button1());
        add(button2());
        add(button3());
        add(button4());
        add(button5());
        add(button6());
    }

    public JButton button1() {
        JButton button = new JButton("查询所有获奖信息");
        button.addActionListener(l -> {
            olympicTable.showData(DbFactory.loadAllData());
        });
        return button;
    }

    public JButton button2() {
        JButton button = new JButton("中国再添一枚金牌");
        button.addActionListener(l -> {
            DbFactory.addGold("中国", 1);
            olympicTable.showData(DbFactory.loadAllData());
        });
        return button;
    }

    public JButton button3() {
        JButton button = new JButton("补充德国代表队的获奖信息");
        button.addActionListener(l -> {
            DbFactory.insert("德国", 12, 10, 5);
            olympicTable.showData(DbFactory.loadAllData());
        });
        return button;
    }

    public JButton button4() {
        JButton button = new JButton("查询获得9枚金牌的代表队");
        button.addActionListener(l -> {
            olympicTable.showData(DbFactory.getByMedal(9, 0, 0));
        });
        return button;
    }

    @SneakyThrows
    public JButton button5() {
        JButton button = new JButton("查询冰岛代表队");
        button.addActionListener(l -> {
            Vector<Vector<Object>> rowData = new Vector<>();
            rowData.add(DbFactory.getByQueueName("冰岛"));
            olympicTable.showData(rowData);
        });
        return button;
    }

    @SneakyThrows
    public JButton button6() {
        JButton button = new JButton("删除没有获得奖牌的代表队");
        button.addActionListener(l -> {
            DbFactory.deleteNone();
            olympicTable.showData(DbFactory.loadAllData());
        });
        return button;
    }
}
